package cn.joyfulmedia.controller.system.dict;


import cn.joyfulmedia.common.util.Const;
import cn.joyfulmedia.common.util.page.Pagination;
import cn.joyfulmedia.entity.ExamMisDict;
import cn.joyfulmedia.mis.service.interfaces.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/dict")
public class DictController {
    @Resource
    private DictService dictService;

    @RequestMapping("/list")
    public String list(Pagination pagination, ExamMisDict dict, Model model) {
        List<ExamMisDict> list = dictService.listPage(pagination, dict);
        model.addAttribute("list", list);
        model.addAttribute(Const.PAGINATION, pagination);
        return "/dict/list";
    }

    @ResponseBody
    @RequestMapping("/getById/{id}")
    public Object getById(@PathVariable Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Const.STATUS, -1);
        try {
            ExamMisDict a = dictService.selectDictById(id);
            map.put(Const.STATUS, 0);
            map.put("data", a);
        } catch (Exception e) {
            e.printStackTrace();
            map.put(Const.MSG, Const.DEFAULT_ERROR);
        }
        return map;
    }

    @RequiresPermissions("sys:dict:add")
    @RequestMapping("/add")
    public String add(Long id, Model model) {
        model.addAttribute("id", id);
        return "/dict/add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(ExamMisDict dict) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Const.STATUS, -1);
        try {
            Integer i = 0;
            if (dict.getId() != null && dict.getId() != 0) {
                i = dictService.updateDictById(dict);
            } else {
                i = dictService.saveDict(dict);
            }
            if (i > 0) {
                map.put(Const.STATUS, 0);
                map.put("data", "保存成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put(Const.MSG, Const.DEFAULT_ERROR);
        }
        return map;
    }
}
